package com.my.ads.lib.listen;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.BatteryManager;

import com.my.ads.lib.Config;
import com.my.ads.lib.bean.DataBean;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by xuanyang on 2018/3/8.
 */

public class PowerInfoReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        try {
            if (Intent.ACTION_TIME_TICK.equals(intent.getAction())) {
                EventBus.getDefault().post(new DataBean(Config.EventTime, 0, 0));
            } else if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) {
                int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
                int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 0);
                int levelPercent = (int) (((float) level / scale) * 100);
                int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_UNKNOWN);
                EventBus.getDefault().post(new DataBean(Config.EventPower, levelPercent, status));
            } else if (Intent.ACTION_DATE_CHANGED.equals(intent.getAction())) {
                EventBus.getDefault().post(new DataBean(Config.EventDate, 0, 0));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
